﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common;
using Common.Controls;
using Common.Admin;

namespace SLAdmin.UserControls
{
    public partial class EditStudies : UserControl
    {
        List<Study> _studies = new List<Study>();
        App _myApp = App.GetApplication;
        StudyManager _studyManager = StudyManager.GetInstance();
        StudyDataClient _studyDataClient = new StudyDataClient();
        DialogHelper _dialogHelper = new DialogHelper();

        public delegate void StudiesUpdatedDelegate();
        public event StudiesUpdatedDelegate StudiesUpdated;

        public EditStudies()
        {
            InitializeComponent();
        }
        public EditStudies(List<Study> Studies)
        {
            InitializeComponent();
            _studies = Studies;
            this.DataContext = _studies;            
            StudyGrid.ItemsSource = _studies;
            _studyDataClient.CreateStudyCompleted += new StudyDataClient.CreateStudyCompletedDelegate(_myApp_CreateStudyCompleted);            
            _dialogHelper.DialogAccepted += new DialogHelper.DialogAcceptedDelegate(_myApp_DialogAccepted);
            _studyDataClient.DeleteStudyCompleted += new StudyDataClient.DeleteStudyCompletedDelegate(_myApp_DeleteStudyCompleted);
        }

        void _myApp_DeleteStudyCompleted(Study Study)
        {
            StudyGrid.SelectedItem = null;
            _studies.Remove(Study);            
            StudyGrid.ItemsSource = null;
            StudyGrid.ItemsSource = _studies;                        
            StudiesUpdated();
        }

        void _myApp_DialogAccepted(object DataContext)
        {            
            Study myStudy = DataContext as Study;
            _studyDataClient.DeleteStudy(myStudy);      
      
        }

        void _myApp_CreateStudyCompleted(Study Study)
        {
            StudyGrid.SelectedItem = null;
            if (_studies.Contains(Study) == false)
            {
                _studies.Add(Study);
            }            
            StudyGrid.ItemsSource = null;
            StudyGrid.ItemsSource = _studies;
            StudiesUpdated();
        }

        private void btnNewStudy_Click(object sender, RoutedEventArgs e)
        {
            _studyDataClient.CreateStudy();
        }

        private void btnStudyDelete_Click(object sender, RoutedEventArgs e)
        {
            Study MyStudy = (sender as Button).DataContext as Study;
            _dialogHelper.Alert("Confirm delete", "Are you sure you want to delete this study?", MyStudy,LayoutRoot);                              
        }

        private void UserControl_LayoutUpdated(object sender, EventArgs e)
        {
            StudyGrid.Width = this.ActualWidth - 20;
            
        }
    }
}
